package com.ovopark.organize.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesRegistrationContentMapper;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationContent;
import com.ovopark.organize.server.model.query.RegistrationFieldQuery;
import com.ovopark.organize.server.model.vo.FranchiseesRegistrationFieldVo;
import com.ovopark.organize.server.model.vo.SimpleVo;
import com.ovopark.organize.server.service.FranchiseesRegistrationContentService;
import com.ovopark.organize.server.util.CollectionUtil;
import com.ovopark.organize.server.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FranchiseesRegistrationContentServiceImpl extends ServiceImpl<FranchiseesRegistrationContentMapper, FranchiseesRegistrationContent> implements FranchiseesRegistrationContentService {

    Logger logger = LoggerFactory.getLogger(FranchiseesRegistrationContentServiceImpl.class);

    @Override
    public void saveOrUpdateByRegistrationId(Integer registrationId, List<FranchiseesRegistrationFieldVo> fieldVo,Integer userId,Integer groupId) {

        // 如果有 多选，先删除 再插入
        if(CollectionUtil.isNotEmpty(fieldVo)){
            List<Integer> fieldIds = fieldVo.stream().filter(o-> o.getIsMultiple() !=null && o.getIsMultiple().intValue() ==1 ).map(FranchiseesRegistrationFieldVo::getFieldId).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(fieldIds)){
                LambdaQueryWrapper<FranchiseesRegistrationContent> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(FranchiseesRegistrationContent::getRegistrationId,registrationId);
                queryWrapper.in(FranchiseesRegistrationContent::getRegistrationFieldId,fieldIds);
                baseMapper.delete(queryWrapper);
            }
        }



        List<FranchiseesRegistrationContent> oldContent =  getRegistrationContentByRegistrationId(registrationId);
        List<Integer> needDeletedId = new ArrayList<>();
        Map<Integer,FranchiseesRegistrationContent> oldContentMap = new HashMap<>();
        if(CollectionUtil.isNotEmpty(oldContent)){
           // needDeletedId = oldContent.stream().map(FranchiseesRegistrationContent::getId).collect(Collectors.toList());
            for (FranchiseesRegistrationContent registr:  oldContent ) {
                 Integer contentId = registr.getId();
                needDeletedId.add(contentId);
                oldContentMap.put(contentId,registr);
                
            }

        }
        if(CollectionUtil.isNotEmpty(fieldVo)){

            for (FranchiseesRegistrationFieldVo vo:fieldVo ) {
                Integer id = vo.getId();
                FranchiseesRegistrationContent content = null;
                if(id !=null){
                    needDeletedId.remove(id);
                    content = oldContentMap.get(id);
                }
                if(content ==null){
                    content =  new FranchiseesRegistrationContent();
                    content.setCreateId(userId);
                    content.setCreateTime(new Date());
                    content.setRegistrationId(registrationId);
                    content.setGroupId(groupId);
                }
                content.setRegistrationFieldId(vo.getFieldId());
                if(vo.getIsMultiple() ==1){
                    List<SimpleVo> multipleValue =   vo.getMultipleValue();
                    //  重新插入
                    if(CollectionUtil.isNotEmpty(multipleValue)){
                        for (SimpleVo simpleVo:multipleValue ) {
                            FranchiseesRegistrationContent multipleContent = new FranchiseesRegistrationContent();
                            BeanUtils.copyProperties(content,multipleContent);
                            multipleContent.setCreateId(userId);
                            multipleContent.setCreateTime(new Date());
                            multipleContent.setRegistrationId(registrationId);
                            multipleContent.setGroupId(groupId);
                            multipleContent.setRegistrationFieldId(vo.getFieldId());
                            multipleContent.setRegistrationFieldSelectId(simpleVo.getId());
                            saveRegistrationContent(multipleContent);
                        }
                    }
                  /*  Integer fieldSelectId = null;
                    if(StringUtils.isNotEmpty(vo.getValue())){
                        fieldSelectId = Integer.valueOf(vo.getValue());
                    }
                    content.setRegistrationFieldSelectId(fieldSelectId);*/
                }else {
                    content.setContent(vo.getValue());
                    saveRegistrationContent(content);
                }


            }

        }

        if(CollectionUtil.isNotEmpty(needDeletedId)){
            deletedRegistrationContentByIds(needDeletedId);
        }





    }

    @Override
    public List<FranchiseesRegistrationContent> getRegistrationContentByRegistrationId(Integer registrationId) {
        LambdaQueryWrapper<FranchiseesRegistrationContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesRegistrationContent::getRegistrationId,registrationId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public FranchiseesRegistrationContent saveRegistrationContent(FranchiseesRegistrationContent content) {
        if(content.getId() !=null){
            baseMapper.updateById(content);
        }else {
            baseMapper.insert(content);
        }
        return content;
    }

    @Override
    public void deletedRegistrationContentByIds(List<Integer> ids) {
         baseMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Integer> getRegistrationIdByQuery(Integer groupId, List<RegistrationFieldQuery> fieldIds) {
        List<Integer> registrationIds = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(fieldIds)){
            for (RegistrationFieldQuery query:fieldIds ) {
               LambdaQueryWrapper<FranchiseesRegistrationContent> queryWrapper = new LambdaQueryWrapper<>();
               queryWrapper.eq(FranchiseesRegistrationContent::getGroupId,groupId);
               if(query.getMultiple()){
                   List<Integer> selectIds = CommonUtil.createList(query.getValue());
                   queryWrapper.eq(FranchiseesRegistrationContent::getRegistrationFieldId,query.getFieldId());
                   queryWrapper.in(FranchiseesRegistrationContent::getRegistrationFieldSelectId,selectIds);
               }else {
                   queryWrapper.eq(FranchiseesRegistrationContent::getRegistrationFieldId,query.getFieldId());
                   queryWrapper.eq(FranchiseesRegistrationContent::getContent,query.getValue());
               }
                if(CollectionUtil.isNotEmpty(registrationIds)){
                    queryWrapper.in(FranchiseesRegistrationContent::getRegistrationId,registrationIds);
                }
                List<FranchiseesRegistrationContent> list = baseMapper.selectList(queryWrapper);
                if(CollectionUtil.isNotEmpty(list)){
                    registrationIds = list.stream().map(FranchiseesRegistrationContent::getRegistrationId).collect(Collectors.toList());
                }else {
                    return new ArrayList<>();
                }
            }
        }

        return registrationIds;
    }

    @Override
    public void updateFieldIdByOldFieldId(Integer fieldId, Integer newFieldId, Integer groupId, Integer oldSelectId, Integer selectId) {
        if((fieldId !=null && newFieldId !=null && groupId !=null) || (oldSelectId !=null && selectId !=null && groupId !=null)){
            baseMapper.updateFieldIdByOldFieldId(fieldId,newFieldId,groupId,oldSelectId,selectId);
        }

    }


}
